<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>Card No</title>
    <link rel="stylesheet" href="/static/css/bootstrap.min.css">
    <link rel="stylesheet" href="/static/css/jquery.dataTables.min.css">
    <link rel="stylesheet" href="/static/css/dataTables.bootstrap.min.css">
    <script type="text/javascript" src="/static/js/jquery-2.1.1.js"></script>
    <script type="text/javascript" src="/static/js/jquery.validate.js"></script>
    <script type="text/javascript" src="/static/js/bootstrap.min.js"></script>
    <script type="text/javascript" src="/static/js/jquery.dataTables.min.js"></script>
    <script type="text/javascript" src="/static/js/dataTables.bootstrap.min.js"></script>
    <script>
        function pad(num, len) {
            var pad_len = len - num.toString().length;
            return Array(pad_len + 1).join(0) + num;  //中间n-1-6位随机数，若不足位前面补0
        }

        function cardno(card_bin, card_length, card_count) {
            var result = [];
            var length = parseInt(card_length) - card_bin.length - 1;  //中间随机数的位数
            var count = 0;
            for (count = 0; count < parseInt(card_count); count++) {
                var random_part = pad(Math.floor(Math.random() * Math.pow(10, length) - 1), length);
                var precard = card_bin + random_part;
                var plus = 0, num = 0, tail = 0;

                for (var n = precard.length - 1; n >= 0; n--) {
                    num += 1;
                    if (num % 2 > 0) {
                        var odd = parseInt(precard.charAt(n)) * 2;
                        for (var idx = 0; idx < odd.toString().length; idx++) {
                            plus += parseInt(odd.toString().charAt(idx));
                        }
                    }
                    if (num % 2 == 0) {
                        plus += parseInt(precard.charAt(n));
                    }
                }
                if (plus % 10 == 0) {
                    tail = 0
                } else {
                    tail = 10 - plus % 10;
                }
                result.push(precard + tail);
            }
            return result;
        }

        $(function () {
            $('#cardNo_button').click(function () {
                var tableData = new Array();
                $('#myform').validate({
                    rules: {
                        card_bin: "required"
                    }
                });
                var card_nums = cardno($('#card_bin').val(), $('#card_length').val(), $('input:radio:checked').val());
                otable = $('#table').dataTable({
                    "oLanguage": {
                        "sProcessing": "正在加载中......",
                        "sLengthMenu": "每页显示 _MENU_ 条记录",
                        "sZeroRecords": "对不起，查询不到相关数据！",
                        "sEmptyTable": "表中无数据存在！",
                        "sInfo": "当前显示 _START_ 到 _END_ 条，共 _TOTAL_ 条记录",
                        "sInfoFiltered": "数据表中共为 _MAX_ 条记录",
                        "sSearch": "模糊查询",
                        "oPaginate": {
                            "sFirst": "首页",
                            "sPrevious": "上一页",
                            "sNext": "下一页",
                            "sLast": "末页"
                        }
                    }
                });
                for (var idx = 0; idx < card_nums.length; idx++) {
                    tableData.push([card_nums[idx]]);
                }
                otable.fnClearTable();
                otable.fnAddData(tableData);
            });
            cardBIN_table = $('#cardBINtab').dataTable({
                "oLanguage": {
                    "sProcessing": "正在加载中......",
                    "sLengthMenu": "每页显示 _MENU_ 条记录",
                    "sZeroRecords": "对不起，查询不到相关数据！",
                    "sEmptyTable": "表中无数据存在！",
                    "sInfo": "当前显示 _START_ 到 _END_ 条，共 _TOTAL_ 条记录",
                    "sInfoFiltered": "数据表中共为 _MAX_ 条记录",
                    "sSearch": "模糊查询",
                    "oPaginate": {
                        "sFirst": "首页",
                        "sPrevious": "上一页",
                        "sNext": "下一页",
                        "sLast": "末页"
                    }
                }
            });
            $.ajax({
                url: "cardBIN",
                success: function (data) {
                    cardBIN_table.fnAddData(data);
                }
            });
        });
    </script>
</head>
<body>
{% include "header.html" %}


<ul class="nav nav-tabs" style="margin-top: 70px">
    <li class="active"><a href="#cardno" data-toggle="tab">银行卡号</a></li>
    <li><a href="#cardbin" data-toggle="tab">卡BIN查询</a></li>
</ul>


<div class="tab-content">
    <div id="cardno" class="tab-pane active">
        <form class="form-horizontal" role="form" style="margin: 30px" id="myform">
            <div class="form-group">
                <label for="card_bin" class="col-sm-2 control-label">卡BIN：</label>

                <div class="col-xs-3">
                    <input class="form-control" id="card_bin" name="card_bin" type="text" maxlength="6"></div>
            </div>
            <div class="form-group">
                <label for="card_length" class="col-sm-2 control-label">银行卡长度：</label>

                <div class="col-xs-3">
                    <select class="form-control" id="card_length">
                        <option>15</option>
                        <option>16</option>
                        <option>17</option>
                        <option>18</option>
                        <option>19</option>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-2 control-label">生成多少张卡：</label>

                <div class="radio col-xs-3">
                    <label class="radio-inline">
                        <input type="radio" name="card_count" id="count1" value="10" checked>10</label>
                    <label class="radio-inline">
                        <input type="radio" name="card_count" id="count2" value="20">20</label>
                    <label class="radio-inline">
                        <input type="radio" name="card_count" id="count3" value="30">30</label>
                    <label class="radio-inline">
                        <input type="radio" name="card_count" id="count4" value="100">100</label>
                </div>
            </div>
            <div class="form-group">
                <div class="col-sm-offset-2 col-sm-10">
                    <button type="button" class="btn btn-primary" id="cardNo_button">提交</button>
                    <span>（可根据银行卡的卡BIN和长度随机生成卡号）</span>
                </div>
            </div>
        </form>
        <div class="container">
            <div class="dataTables_wrapper form-inline">
                <table class="table table-striped table-bordered dataTable no-footer" id="table">
                    <thead>
                    <tr>
                        <th>银行卡号</th>
                    </tr>
                    </thead>
                    <tbody>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    <div id="cardbin" class="tab-pane">
        <div class="container">
            <div class="dataTables_wrapper form-inline" style="margin: 30px">
                <table class="table table-striped table-bordered dataTable no-footer" id="cardBINtab">
                    <thead>
                    <tr>
                        <th>发卡行</th>
                        <th>机构代码</th>
                        <th>卡BIN</th>
                        <th>卡号长度</th>
                        <th>卡类型</th>
                    </tr>
                    </thead>
                    <tbody>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
</body>
</html>